Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
"flex: 1 1 0%" with a canvas child that adapts its size is not working properly

The following example creates a header, a canvas, and a footer. The canvas is told to fill the space between header and footer. The following happens:

  • Expand the window: The canvas grows
  • Shrink the window: Scrollbars appear, but I want the canvas to shrink

Why is the canvas not shrinking and how can I make it shrink?

const canvas = document.getElementById("canvas");

function paint() {
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
  const ctx = canvas.getContext("2d");
  ctx.fillText(canvas.height, canvas.width / 2, canvas.height / 2);
}

paint();
new ResizeObserver(paint).observe(canvas);
.main {
  height: 100%;
  display: flex;
  flex-flow: column;
}

.canvas {
  flex: 1 0 0%;
}

html, body {
  height: 100%;
  margin: 0;
}
<div class="main">
  <div class="header">
    Header
  </div>
  <canvas id="canvas" class="canvas"></canvas>
  <div class="footer">
    Footer
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Setting canvas height to 0 and flex-grow to 1 seems to achieve what you want.

const canvas = document.getElementById("canvas");



function paint() {
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
  const ctx = canvas.getContext("2d");
  ctx.fillText(canvas.height, canvas.width / 2, canvas.height / 2);
}

paint();
new ResizeObserver(paint).observe(canvas);
.main {
  display: flex;
  flex-direction: column;
  height: 100%;
}

canvas {
  height: 0;
  flex-grow: 1;
}

html, body {
  height: 100%;
  margin: 0;
}
<div class="main">
  <div class="header">
    Header
  </div>
  <canvas id="canvas" class="canvas"></canvas>
  <div class="footer">
    Footer
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda